process.env.NODE_ENV = 'production'

const path = require('path')
const webpack = require('webpack')
const merge = require('webpack-merge')
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const baseConf = require('./webpack.common')

const {
	index,
	PATHS,
	template,
	publicPath,
	pathsToClean,
	cleanOptions,
} = require('./utils')

const isProduction = process.env.NODE_ENV === 'production'

module.exports = merge(baseConf, {

	mode: 'production',

	devtool: 'source-map',

	output: {
		filename: PATHS.assets("js/[name].[hash:8].js"),
		chunkFilename: PATHS.assets('js/[name].[chunkhash].js'),
		path: PATHS.output,
		publicPath,
	},

	plugins: [
		new CleanWebpackPlugin(pathsToClean, cleanOptions),

		new CopyWebpackPlugin([{
			from: path.join(PATHS.src, 'favicon.ico'),
			to: path.join(PATHS.public, 'favicon.ico')
		}]),

		new webpack.DefinePlugin({
			'process.env.NODE_ENV': JSON.stringify('production')
		}),

	],

	optimization: {
		minimizer: [
			new UglifyJsPlugin({
				cache: true,
				parallel: true,
				sourceMap: true // set to true if you want JS source maps
			}),
			new OptimizeCSSAssetsPlugin({})
		],
		splitChunks: {
			// automaticNameDelimiter: "-",
			chunks: "initial",
			cacheGroups: {
				commons: {
					// chunks: "initial",
					minChunks: 2,
					maxInitialRequests: 5, // The default limit is too small to showcase the effect
					minSize: 0 // This is example is too small to create commons chunks
				},
				utils: {
					test: /utils/,
					// chunks: "initial",
					name: "utils",
					enforce: true
				},
				vendor: {
					test: /node_modules/,
					// chunks: "initial",
					name: "vendor",
					priority: 10,
					enforce: true
				},
				styles: {
					name: 'style',
					test: /\.css$/,
					chunks: 'all',
					enforce: true
				},
			}
		},
		runtimeChunk: {
			name: "manifest"
		},
	},

})